<?php
// 修改商品信息
require $this->website['path']['action'] . '/admin/isedit.php';

$json = array('status' => 'error', 'msg' => '', 'data' => []);

$id = fn_post('id');
$data = fn_post('data');

$data = json_decode($data, 1);
//data['product']
//data['image']
 
//data['detail']

if (!$id || !is_numeric($id)) {
	$json['msg'] = '错误的编号';
} elseif (!$this->website['class']['db']
		->table('product')
		->where('id=?', $id)
		->limit(1)
		->count()) {
	$json['msg'] = '商品不存在或已删除';
} else {
	// product
	if (count($data['product'])) {
		$field = array_keys($data['product']);
		$value = array_values($data['product']);

		$this->website['class']['db']
			->table('product')
			->field($field)
			->value($value)
			->where('id=?', $id)
			->limit(1)
			->update();
	}
	// image
	if (count($data['image'])) {
		$this->website['class']['db']
			->table('product_image')
			->where('pid=?', $id)
			->delete();

		foreach ($data['image'] as $v) {
			$this->website['class']['db']
				->table('product_image')
				->field('pid', 'image')
				->value($id, $v)
				->insert();
		}
	}
	// detail
	if (count($data['detail'])) {
		$this->website['class']['db']
			->table('product_detail')
			->where('pid=?', $id)
			->delete();
		foreach ($data['detail'] as $v) {
			$this->website['class']['db']
				->table('product_detail')
				->field('pid', 'image')
				->value($id, $v)
				->insert();
		}
	}

 

	$json['status'] = 'ok';
	$json['msg'] = '修改成功';
	$json['data'] = $this->website['class']['db']->sql;

}

$this->json_encode($json);
